<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="二维码ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入二维码ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家名称" prop="shopId">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商家名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二维码类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择二维码类型" clearable size="small">
          <el-option label="A类" value="A" />
          <el-option label="B类" value="B" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createdTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['union:qrcode:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qrcodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="二维码ID" align="center" prop="qrcode_shop_id" />
      <el-table-column label="商家名称" align="center" prop="shop_name" :formatter="formatShopName" />
      <el-table-column label="二维码类型" align="center" prop="type" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listQrcode, getQrcode, delQrcode, addQrcode, updateQrcode, exportQrcode } from "@/api/union/qrcode";

export default {
  name: "Qrcode",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联盟商家的二维码表格数据
      qrcodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        // orderByColumn: "id",
        // isAsc: "desc",
        qrcodeShopId: null,
        shopId: null,
        type: null,
        createdTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qrcodeShopId: [
          { required: true, message: "二维码上体现的商户 ID,可预创建不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "二维码类型：A类，B类不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询联盟商家的二维码列表 */
    getList() {
      this.loading = true;

      if (this.queryParams.createdTime) {
        this.queryParams.startTime = this.queryParams.createdTime[0]
        this.queryParams.endTime = this.queryParams.createdTime[1]
      }

      listQrcode(this.queryParams).then(response => {
        this.qrcodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/union/qrcode/list");
    },
    formatShopName(row) {
      if (row.shop_name === null) {
        return '暂未指定商家';
      } else {
        return row.shop_name;
      }
    }
  }
};
</script>
